/***********************************************
 This file is part of redia-lib-utils-ios
 
 
 Copyright (c) 2015 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

#import "UIImage+MultiplyColor.h"

@implementation UIImage (MultiplyColor)

+(UIImage *)imageNamed:(NSString *)name multiplyWithColor:(UIColor *)color
{
    return [self image:[self imageNamed:name] multiplyWithColor:color];
}

+(UIImage *)image:(UIImage *)originalImage multiplyWithColor:(UIColor *)color
{
    return [self image:originalImage multiplyWithColor:color newScale:1.0];
}

+(UIImage *)image:(UIImage *)originalImage multiplyWithColor:(UIColor *)color newScale:(CGFloat)newScale
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(originalImage.size.width*newScale, originalImage.size.height*newScale), NO, originalImage.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0, originalImage.size.height*newScale);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGRect rect = CGRectMake(0, 0, originalImage.size.width*newScale, originalImage.size.height*newScale);
    
    
    CGContextSetBlendMode (context, kCGBlendModeMultiply);
    
    CGContextDrawImage(context, rect, originalImage.CGImage);
    
    CGContextClipToMask(context, rect, originalImage.CGImage);
    
    [color setFill];
    CGContextFillRect(context, rect);
    
    UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return coloredImage;

}

+(UIImage*)image:(UIImage *)originalImage multipliedOntoColor:(UIColor *)color usingAlpha:(CGFloat)alpha
{
    CGFloat originalScale = originalImage.scale;
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(originalImage.size.width*originalScale, originalImage.size.height*originalScale), NO, originalScale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0, originalImage.size.height*originalScale);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGRect rect = CGRectMake(0, 0, originalImage.size.width*originalScale, originalImage.size.height*originalScale);
    
    [color setFill];
    CGContextFillRect(context, rect);
    
    CGContextSetBlendMode (context, kCGBlendModeMultiply);

    CGContextSetAlpha(context, alpha);
    
    CGContextDrawImage(context, rect, originalImage.CGImage);
    
    //CGContextClipToMask(context, rect, originalImage.CGImage);
    
    
    UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return coloredImage;
}
@end
